package com.yun.test03generate.common;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

public abstract class BaseService<M extends BaseMapper, E extends BaseExtMapper, T extends Entity, Q extends QueryConditionBase> {
    @Autowired
    public M baseMapper;
    @Autowired
    public E baseExtMapper;

    public BaseService() {
    }

    public M getBaseMapper() {
        return this.baseMapper;
    }

    public E getExtMapper() {
        return this.baseExtMapper;
    }

    public <T> T listOne(List<T> list) {
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

/*    public int create(T entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(this.generateKey());
        }

        return this.getBaseMapper().insertSelective(entity);
    }

    public int update(T entity) {
        //ParameterUtils.checkArgument(entity.getId(), "数据Id");
        T old = this.queryById(entity.getId());
        if (old == null) {
            throw new CommonException("无该数据");
            //throw new ValidateException(RuntimeCodeEnum.NO_DATA_ERROR, RuntimeCodeEnum.NO_DATA_ERROR.getMessage());
        } else {
            int ret = this.getBaseMapper().updateByPrimaryKeySelective(entity);
            if (ret == 0) {
                throw new CommonException("无该数据");
                //throw new ValidateException(RuntimeCodeEnum.UNKNOWN_ERROR, "数据操作失败!");
            } else {
                return ret;
            }
        }
    }

    public int createOrUpdate(T entity) {
        T old = this.queryById(entity.getId());
        if (old == null) {
            if (StringUtils.isBlank(entity.getId())) {
                entity.setId(this.generateKey());
            }

            return this.getBaseMapper().insertSelective(entity);
        } else {
            return this.update(entity);
        }
    }

    public String generateKey() {
        return IdWorker.get32UUID();
    }

    public int deleteById(String key) {
        //ParameterUtils.checkArgument(key, "数据Id");
        int ret = this.getBaseMapper().deleteByPrimaryKey(key);
        if (ret == 0) {
            throw new CommonException("无该数据");
            //throw new ValidateException(RuntimeCodeEnum.NO_DATA_ERROR, RuntimeCodeEnum.NO_DATA_ERROR.getMessage());
        } else {
            return ret;
        }
    }

    public T queryById(String key) {
        return StringUtils.isBlank(key) ? null : (T) this.getBaseMapper().selectByPrimaryKey(key);
    }

    public Page<T> queryPage(Q query) {
        query.generatePageInfo();
        return this.baseExtMapper.selectPage(query);
    }

    public List<T> queryList(Q query) {
        return this.baseExtMapper.selectPage(query);
    }

    public T queryOne(Q query) {
        Integer pPageSize = query.getPageSize();
        Integer pCurrentPage = query.getCurrentPage();
        query.setPageFlag(true).setPageSize(1).setCurrentPage(1).generatePageInfo();
        List<T> list = this.queryList(query);
        query.setPageSize(pPageSize).setCurrentPage(pCurrentPage);
        return (T) this.listOne(list);
    }

    public boolean exists(Q query) {
        T s = this.queryOne(query);
        return s != null;
    }

    public boolean deleteByIds(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new CommonException("无该数据");
            //throw new ValidateException(RuntimeCodeEnum.ILLEGAL_ARGUMENT, "参数数据为空");
        } else {
            int ret = this.baseMapper.deleteByIds(ids);
            if (ret == 0) {
                throw new CommonException("无该数据");
                //throw new ValidateException(RuntimeCodeEnum.NO_DATA_ERROR, RuntimeCodeEnum.NO_DATA_ERROR.getMessage());
            } else {
                return true;
            }
        }
    }*/
}
